<?php use_helper('jQuery')?>
<?php use_stylesheets_for_form($form) ?>
<?php use_javascripts_for_form($form) ?>

<?php echo jq_javascript_tag() ?>
    var features =<?php echo $form['FeaturesBlock']['Features']->count() ?>;

    function addFeature(num) {
        var r = $.ajax({
            type: 'POST',
            url: '<?php echo url_for('@element_addfeatureform'.(!$form->getObject()->isNew() ?
                            '?id='.$form->getObject()->getId().
                            '&portal_id='.$form->getObject()->getSection()->getPortalId().
                            '&num='
                        :
                            '?type_id='.$form->getObject()->getType()->getId().
                            '&portal_id='.$form->getObject()->getSection()->getPortalId().
                            '&num='))?>'+num,
            async: false}).responseText;
        return r;
    }

    function removeFeature(elem){
        $(elem).fadeOut('slow', function(){
            $(this).remove();
        });
    }
    <?php echo 'initElementFeaturesBlockFileUpload("'.
                url_for('element_tempfileupload',$form->getObject()->getType()).
                '?section_id='.$form->getObject()->getSectionId().'&portal_id='.
                $form->getObject()->getSection()->getPortalId().'");'
        ?>
    featuresblockBindEventActions();
<?php echo jq_end_javascript_tag()?>

<div id="main_header" class="ui-widget-header ui-corner-all">
    <?php if($form->getObject()->exists()):?>
        <h3>Editando Bloque de Destacados: "<?php echo $form->getObject()->getTitle()?>"</h3>
    <?php else:?>
        <h3>Nuevo Bloque de Destacados</h3>
    <?php endif;?>
</div>
<div id="main_content" class="ui-widget ui-widget-content ui-corner-all">
    <div class="form">
        <?php echo jq_form_remote_tag(
                array(
                    'update' => 'main',
                    'url' => ($form->getObject()->isNew() ?
                                url_for('element_create').'?type_id='.$type->getId().'&section_id='.$section->getId()
                                :
                                url_for('element_update',$form->getObject())
                                ),
                    'before'   => 'hideMainContent();showLoaderMain();',
                    'complete' => 'hideLoader();showMainContent();',
                    'script'   => true
                ),array(
                    'id' => 'file_upload',
                    'class' => 'form_featuresblock'
                ))?>
        <?php if (!$form->getObject()->isNew()): ?>
            <input type="hidden" name="sf_method" value="put" />
        <?php endif; ?>
            <fieldset>
                <?php echo $form->renderHiddenFields() ?>
                <?php echo $form->renderGlobalErrors() ?>
                <div class="form-row">
                    <?php echo $form['title']->renderLabel() ?>
                    <div class="form-field">
                        <?php echo $form['title']->renderError() ?>
                        <?php echo $form['title']->render() ?>
                    </div>
                </div>
                <div class="form-row">
                    <?php echo $form['FeaturesBlock']['render_title']->renderLabel() ?>
                    <div class="form-field">
                        <?php echo $form['FeaturesBlock']['render_title']->renderError() ?>
                        <?php echo $form['FeaturesBlock']['render_title']->render() ?>
                    </div>
                </div>
                <div class="form-row">
                    <?php echo $form['FeaturesBlock']['uses_bullet']->renderLabel() ?>
                    <div class="form-field">
                        <?php echo $form['FeaturesBlock']['uses_bullet']->renderError() ?>
                        <?php echo $form['FeaturesBlock']['uses_bullet']->render() ?>
                    </div>
                </div>
                <div id="featuresblock_bullet" class="form-row" style="display:
                    <?php echo ($form->getObject()->getFeaturesBlock()->getUsesBullet() ? 'block' : 'none')?>">
                    <?php echo $form['FeaturesBlock']['bullet']->renderError() ?>
                    <?php echo $form['FeaturesBlock']['bullet_type']->renderError() ?>
                    <?php echo $form['FeaturesBlock']['bullet_size']->renderError() ?>
                    <?php echo $form['FeaturesBlock']['tmp_bullet']->renderError() ?>
                    <table id="<?php echo 'files_bullet'?>" class="upload-files-table">
                        <?php if ($form['FeaturesBlock']['bullet']->getValue()): ?>
                            <tr>
                                <td>Archivo actual:</td>
                                <td class="image-preview">
                                    <img alt="<?php echo $form['FeaturesBlock']['bullet']->getValue()?>"
                                         src="<?php echo '/uploads/portal_'.$form->getObject()->getSection()->getPortalId().
                                            ($form->getEmbeddedForm('FeaturesBlock')->getObject()->getBullet() ?
                                                (!$form->isBound() ?
                                                    '/images/'
                                                    :
                                                    ($form['FeaturesBlock']['bullet']->getValue()
                                                        == $form->getEmbeddedForm('FeaturesBlock')->getObject()->getBullet()
                                                        ?
                                                        '/images/'
                                                        :
                                                        '/temp_files/'
                                                    )
                                                )
                                                :
                                                '/temp_files/'
                                            ).
                                            basename($form['FeaturesBlock']['bullet']->getValue())
                                    ?>">
                                </td>
                                <td class="bullet-size">
                                    Tamaño del archivo: <?php echo round(($form['FeaturesBlock']['bullet_size']->getValue()/1024),2) ?> Kb
                                </td>
                            </tr>
                        <?php endif;?>
                    </table>
                    <?php echo $form['FeaturesBlock']['tmp_bullet']->renderLabel() ?>
                    <div id="<?php echo 'drop_zone_bullet'?>" class="form-field drop-zone">
                        <?php echo $form['FeaturesBlock']['tmp_bullet']->render() ?>
                        <div>Archivo a cargar</div>
                    </div>
                    <?php echo $form['FeaturesBlock']['tmp_bullet']->renderHelp() ?>
                </div>
                <div id="featuresblock_bullet_new" class="form-row" style="display:
                    <?php echo ($form->getObject()->getFeaturesBlock()->getUsesBullet() ? 'block' : 'none')?>">
                    <?php echo $form['FeaturesBlock']['bullet_new']->renderError() ?>
                    <?php echo $form['FeaturesBlock']['bullet_new_type']->renderError() ?>
                    <?php echo $form['FeaturesBlock']['bullet_new_size']->renderError() ?>
                    <?php echo $form['FeaturesBlock']['tmp_bullet_new']->renderError() ?>
                    <table id="<?php echo 'files_bullet_new'?>" class="upload-files-table">
                        <?php if ($form['FeaturesBlock']['bullet_new']->getValue()): ?>
                            <tr>
                                <td>Archivo actual:</td>
                                <td class="image-preview">
                                    <img alt="<?php echo $form['FeaturesBlock']['bullet_new']->getValue()?>"
                                         src="<?php echo '/uploads/portal_'.$form->getObject()->getSection()->getPortalId().
                                            ($form->getEmbeddedForm('FeaturesBlock')->getObject()->getBulletNew() ?
                                                (!$form->isBound() ?
                                                    '/images/'
                                                    :
                                                    ($form['FeaturesBlock']['bullet_new']->getValue()
                                                        == $form->getEmbeddedForm('FeaturesBlock')->getObject()->getBulletNew()
                                                        ?
                                                        '/images/'
                                                        :
                                                        '/temp_files/'
                                                    )
                                                )
                                                :
                                                '/temp_files/'
                                            ).
                                            basename($form['FeaturesBlock']['bullet_new']->getValue())
                                    ?>">
                                </td>
                                <td class="bullet_new-size">
                                    Tamaño del archivo: <?php echo round(($form['FeaturesBlock']['bullet_new_size']->getValue()/1024),2) ?> Kb
                                </td>
                            </tr>
                        <?php endif;?>
                    </table>
                    <?php echo $form['FeaturesBlock']['tmp_bullet_new']->renderLabel() ?>
                    <div id="<?php echo 'drop_zone_bullet_new'?>" class="form-field drop-zone">
                        <?php echo $form['FeaturesBlock']['tmp_bullet_new']->render() ?>                        
                        <div>Archivo a cargar</div>
                    </div>
                    <?php echo $form['FeaturesBlock']['tmp_bullet_new']->renderHelp() ?>
                </div>
                <?php if($form->hasGlobalErrors()): ?>
                    <?php echo  $form['FeaturesBlock']['Features']->renderError() ?>
                <?php endif; ?>
                <?php foreach ($form['FeaturesBlock']['Features'] as $i => $feature): ?>
                    <fieldset id="<?php echo sprintf('feature_%d', $i)?>" class="<?php echo ($i == 0 ? 'first' : '') ?>">
                        <legend>Destacado</legend>
                        <div class="form-row">
                            <?php echo $feature['type_id']->renderLabel() ?>
                            <div class="form-field">
                                <?php echo $feature['type_id']->render() ?>
                                <?php echo $feature['type_id']->renderError() ?>
                            </div>
                        </div>
                        <div class="form-row" id="select_feature_<?php echo $i?>">
                            <?php echo $feature['content_id']->renderLabel() ?>
                            <div class="form-field">
                                <?php echo $feature['content_id']->renderError() ?>
                                <?php echo $feature['content_id']->render() ?>
                            </div>
                        </div>
                        <div class="form-row">
                            <?php echo $feature['is_new']->renderLabel() ?>
                            <div class="form-field">
                                <?php echo $feature['is_new']->render() ?>
                                <?php echo $feature['is_new']->renderError() ?>
                            </div>
                        </div>
                        <?php if(isset ($feature['is_enabled'])):?>
                            <div class="form-row">
                                <?php echo $feature['is_enabled']->renderLabel() ?>
                                <div class="form-field">
                                    <?php echo $feature['is_enabled']->renderError() ?>
                                    <?php echo $feature['is_enabled']->render() ?>
                                </div>
                            </div>
                        <?php else: ?>
                            <?php if($i>0):?>
                                <a class="remove" href="#">Eliminar</a>
                            <?php endif;?>
                        <?php endif;?>
                    </fieldset>
                <?php endforeach; ?>
                <div id="extrafeatures"/>
                <div class="add-button">
                    <a class="ui-button ui-widget ui-state-default ui-corner-all ui-button-text-icon-primary"
                       id="add_feature" href="#" aria-disabled="false">
                        <span class="ui-button-icon-primary ui-icon ui-icon-plusthick"></span>
                        <span class="ui-button-text">Agregar</span>
                    </a>
                </div>
                <div class="form-row">
                    <?php echo $form['position']->renderLabel() ?>
                    <div class="form-field">
                        <?php echo $form['position']->renderError() ?>
                        <?php echo $form['position']->render() ?>
                    </div>
                </div>
                <?php if(isset ($form['published'])):?>
                    <div class="form-row">
                        <?php echo $form['published']->renderLabel() ?>
                        <div class="form-field">
                            <?php echo $form['published']->renderError() ?>
                            <?php echo $form['published']->render() ?>
                        </div>
                    </div>
                <?php endif;?>
                <?php if(isset ($form['is_enabled'])):?>
                    <div class="form-row">
                        <?php echo $form['is_enabled']->renderLabel() ?>
                        <div class="form-field">
                            <?php echo $form['is_enabled']->renderError() ?>
                            <?php echo $form['is_enabled']->render() ?>
                        </div>
                    </div>
                <?php endif;?>
            </fieldset>
            <div id="form_buttons" class="ui-widget">
                <input class="ui-button ui-widget ui-state-default ui-corner-all ui-button-text-only ui-button-text"
                       type="submit" value="Guardar" title="Guardar"/>
                <?php if($form->getObject()->isNew()):?>
                    <?php echo jq_link_to_remote(
                        '<span class="ui-button-text">Cancelar</span>',
                        array(
                            'update' => 'main',
                            'url' => url_for('section_show', $form->getObject()->getSection()),
                            'before' => 'clearView();showLoaderMain();',
                            'complete' => 'hideLoader();',
                            'script' => true
                        ),array(
                            'class' => 'ui-button ui-widget ui-state-default ui-corner-all ui-button-text-only',
                            'title' => 'Cancelar'
                        ))?>
                <?php else: ?>
                    <?php echo jq_link_to_remote(
                            '<span class="ui-button-text">Cancelar</span>',
                        array(
                            'update' => 'main',
                            'url' => url_for('element_show',$form->getObject()),
                            'before' => 'clearView();showLoaderMain();',
                            'complete' => 'hideLoader();',
                            'script' => true
                        ),array(
                            'class' => 'ui-button ui-widget ui-state-default ui-corner-all ui-button-text-only',
                            'title' => 'Cancelar'
                        ))?>
                <?php endif; ?>
            </div>
        </form>
    </div>
</div>